import { createReducer } from 'reduxsauce'
import { REHYDRATE } from 'redux-persist/es/constants'
import { GET_VIDEO_LIST } from '@ACTIONS/video'

const INITIAL_STATE = {
  homeVideoList: [],
  loadingHomeVideo: false,
  videoList: [],
  loadingVideo: false,
  videoListTotal: 0,
}

const rehydrate = (state, { payload }) => {
  if (payload && payload.video) {
    return { ...INITIAL_STATE, ...payload.video }
  }
  return { ...state }
}

export default createReducer(INITIAL_STATE, {
  [REHYDRATE]: rehydrate,

  [GET_VIDEO_LIST.REQUEST]: (state, { isInitial }) => {
    const nextState = { loadingVideo: true }
    if (isInitial) {
      nextState.videoList = []
      nextState.videoListTotal = 0
    }
    return { ...state, ...nextState }
  },
  [GET_VIDEO_LIST.SUCCESS]: (state, { data, total }) => ({
    ...state, videoList: data, loadingVideo: false, videoListTotal: total
  }),
  [GET_VIDEO_LIST.FAILURE]: state => ({ ...state, loadingVideo: false }),
})
